
package com.bip.it;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText editUsername;
    private EditText editPassword;
    private Intent openWebView;
    public static HttpClient httpclient = new DefaultHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editUsername = (EditText)findViewById(R.id.editUsername);
        editPassword = (EditText)findViewById(R.id.editPassword);
        Button btnLogin = (Button)findViewById(R.id.btnLogin);
        openWebView = new Intent(this,WebViewActivity.class);
        btnLogin.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick (View v){
                String username = editUsername.getText().toString();
                String password = editPassword.getText().toString();
                HttpPostTask con = new HttpPostTask();
                con.execute(username,password,Boolean.toString(false));
            }
        });
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private class HttpPostTask extends AsyncTask<String, Void, String> {
        private ProgressDialog pd = null;
        @Override
        protected void onPreExecute(){
            pd = ProgressDialog.show(MainActivity.this, "Bip.it", "Caricamento in corso...");
        }
        @Override
        protected String doInBackground(String... args) {
         // Create a new HttpClient and Post Header
            HttpPost httppost = new HttpPost("https://www.bip.it/login/");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                nameValuePairs.add(new BasicNameValuePair("msisdnId", args[0]));
                nameValuePairs.add(new BasicNameValuePair("bippswd", args[1]));
                nameValuePairs.add(new BasicNameValuePair("remember", args[2]));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.i("HttpPostTask.doInBackground()","Username: "+args[0]+", Password: "+args[1]);
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                BasicResponseHandler resp = new BasicResponseHandler();
                return resp.handleResponse(response);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            Log.i("response",result);
            pd.dismiss();
            openWebView.putExtra("htmlString", result);
            startActivity(openWebView);
        }
    }

}
